package com.itcast.tanhuasso.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.itcast.tanhuasso.domain.PicUploadResult;
import com.itcast.tanhuasso.domain.User;
import com.itcast.tanhuasso.domain.UserInfo;
import com.itcast.tanhuasso.enums.SexEnum;
import com.itcast.tanhuasso.handle.FaceEngineHandle;
import com.itcast.tanhuasso.handle.PicUploadHandle;
import com.itcast.tanhuasso.mapper.UserInfoMapper;
import com.itcast.tanhuasso.service.UserInfoService;
import com.itcast.tanhuasso.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

/**
 * @PachageName: com.itcast.tanhuasso.service
 * @ClassName:
 * @Description: TODO
 * @Author: Hushi
 * @DateTime: 2021/2/913:29
 * @Version 1.0
 */
@Service
@Slf4j
public class UserInfoServiceImpl implements UserInfoService {
    @Autowired
    private UserService userService;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private PicUploadHandle picUploadHandle;

    @Autowired
    private FaceEngineHandle faceEngineHandle;
    /**
     * 保存用户信息
     * @param parm 用户信息
     * @param token 登陆状态token
     * @return
     */
    @Override
    public boolean saveUserInfo(Map<String, String> parm, String token) {
        //校验token
        User user = this.userService.queryUserByToken(token);
        if (user == null){
            return false;
        }
        //保存新用户信息
        UserInfo info = new UserInfo();
        info.setUserId(Long.valueOf(user.getId()));
        info.setSex(StringUtils.equalsAnyIgnoreCase(parm.get("gender"),"man") ? SexEnum.MAN : SexEnum.WOMAN);
        info.setNickName(parm.get("nickname"));
        info.setBirthday(parm.get("birthday"));
        info.setCity(parm.get("city"));
        return userInfoMapper.insert(info) == 1;
    }

    @Override
    public boolean saveUserLogo(MultipartFile file, String token) {
        //校验token
        User user = this.userService.queryUserByToken(token);
        if (user == null){
            return false;
        }
        //校验是否是人像
        try {
            boolean result = faceEngineHandle.checkIsPortrait(file.getBytes());
            if (!result){
                return false;
            }
        } catch (IOException exception) {
            log.error("校验人像失败," + exception);
        }
        //上传图片到阿里云OSS
        PicUploadResult picUploadResult = picUploadHandle.upload(file);
        if (StringUtils.isEmpty(picUploadResult.getName())){
            return false; //上传失败
        }

        //保存头像
        UserInfo info = new UserInfo();
        info.setLogo(picUploadResult.getName());
        UpdateWrapper<UserInfo> wrapper = new UpdateWrapper<>();
        wrapper.eq("user_id",user.getId());

        return userInfoMapper.update(info,wrapper) == 1;
    }


}
